class NewBookButton < MenuButton

	def initialize
		super(Gtk::Stock::NEW)
		self.tip = _('Create a new book (Ctrl+N)')
		self.signal_connect('clicked') { NewBookButton.new_book }
	end

	def NewBookButton.new_book(or_open = false)
		a = Gtk::Assistant.new
		a.title = _('Book Wizard')
		a.set_size_request(500, 400)

		# Page 0
		new = Gtk::RadioButton.new(_('Create a new book'))
		open = Gtk::RadioButton.new(new, _('Open a existing book'))
		if or_open
			page0 = Gtk::Alignment.new(0.5, 0.5, 0, 0)
			vbox = Gtk::VBox.new(false, 6)
			new.signal_connect('toggled') { a.set_page_type(page0, Gtk::Assistant::PAGE_INTRO) }
			open.signal_connect('toggled') { a.set_page_type(page0, Gtk::Assistant::PAGE_CONFIRM) }
			vbox.pack_start(new)
			vbox.pack_start(open)
			page0 << vbox
			page0.show_all
			a.append_page(page0)
			a.set_page_type(page0, Gtk::Assistant::PAGE_INTRO)
			a.set_page_title(page0, _('Enter book information'))
			a.set_page_complete(page0, true)
		else
			new.active = true
		end

		# Page 1
		page1 = Gtk::Alignment.new(0.5, 0.5, 0, 0)
		table = Gtk::Table.new(3, 2, false)
		title = Gtk::Entry.new
		subtitle = Gtk::Entry.new
		author = Gtk::Entry.new
		table.column_spacings = table.row_spacings = 6
		table.attach(Gtk::Label.new(_('Title')), 0, 1, 0, 1)
		table.attach(title, 1, 2, 0, 1)
		table.attach(Gtk::Label.new(_('Subtitle')), 0, 1, 1, 2)
		table.attach(subtitle, 1, 2, 1, 2)
		table.attach(Gtk::Label.new(_('Author')), 0, 1, 2, 3)
		table.attach(author, 1, 2, 2, 3)
		page1 << table
		page1.show_all
		a.append_page(page1)
		a.set_page_type(page1, Gtk::Assistant::PAGE_INTRO) if not or_open
		a.set_page_title(page1, _('Enter book information'))
		a.set_page_complete(page1, true)
		
		# Page 2
		page2 = Gtk::FileChooserWidget.new(Gtk::FileChooser::ACTION_SAVE)
		page2.border_width = 12
		page2.do_overwrite_confirmation = true
		filter = Gtk::FileFilter.new
		filter.name = _('BookWrite files (*.bw)')
		filter.add_pattern('*.bw')
		page2.add_filter(filter)
		a.append_page(page2)
		a.set_page_type(page2, Gtk::Assistant::PAGE_CONFIRM)
		a.set_page_title(page2, _('Choose project file'))
		a.set_page_complete(page2, true)

		a.signal_connect('cancel') do
			a.destroy
			Gtk.main_quit if or_open
		end
		a.signal_connect('close') do
			if page2.filename != nil
				$main.save_book
				book = Book.new
				book.title = title.text
				book.subtitle = subtitle.text
				book.authors << author.text
				book.chapters << Chapter.new(_('Chapter 1'))
				$main.filename = page2.filename
				$main.filename += '.bw' if not $main.filename =~ /\.bw$/
				$main.book = book
				a.destroy
			else
				# TODO
			end
		end
		a.signal_connect('apply') do
			if open.active? and a.current_page == 1
				a.destroy
				$main.open_book
				Gtk.main_quit if not $main.book
			end
		end

		a.show_all
	end

end
